//
//  WLLEmoticonTools.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//最多显示的列数行数 和 最多显示表情个数
let WLLEmoticonsMaxCol = 7
let WLLEmoticonsMaxRow = 3
let WLLEmoticonsMaxCount = WLLEmoticonsMaxCol*WLLEmoticonsMaxRow - 1



class WLLEmoticonTools: NSObject {

    ///全局访问点
    static let sharedTools : WLLEmoticonTools = WLLEmoticonTools()
    
       /// 读取 bundle 文件
    lazy var emoticonsBundle : NSBundle = {
        //路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
       
    }()
    
    //默认表情一维数组
    lazy var defaultEmoticons : [WLLEmoticonModel] = {
       return self.getEmoticonsWithPath("default")
    }()
    
    //emoji 表情一维数组
    lazy var emojiEmoticons: [WLLEmoticonModel] = {
        return self.getEmoticonsWithPath("emoji")
    }()
    
    //浪小花表情一维数组
    lazy var lxhEmoticons : [WLLEmoticonModel] = {
       return self.getEmoticonsWithPath("lxh")
    }()
    
    
    //三维数组
    lazy var allEmoticons : [[[WLLEmoticonModel]]] = {
       
        return [
         self.getEmoticonsGroupsWitEmoticons(self.defaultEmoticons),
            self.getEmoticonsGroupsWitEmoticons(self.emojiEmoticons),
            self.getEmoticonsGroupsWitEmoticons(self.lxhEmoticons)
        
        ]
    }()
    
    //一维数组转二维数组
    
    func getEmoticonsGroupsWitEmoticons(emoticons : [WLLEmoticonModel]) -> [[WLLEmoticonModel]] {
        //获取最多显示页数
        let  pageCount = (emoticons.count  - 1)/WLLEmoticonsMaxCount + 1
        //创建一个可变的二维临时数组
        var tempArray : [[WLLEmoticonModel]] = [[WLLEmoticonModel]]()
        //遍历数组
        for i in 0..<pageCount {
            let location = i*WLLEmoticonsMaxCount
            var length = WLLEmoticonsMaxCount
             // 由于默认表情为108哥  遍历到最后一组 [100,20] 而我们只有 到[100,8]
            if location + length > emoticons.count {
                length = emoticons.count - location
            }
            //范围
            let range = NSRange(location: location, length: length)
            //数组截取
             let array = (emoticons as NSArray).subarrayWithRange(range) as! [WLLEmoticonModel]
            //添加一维数组
            tempArray.append(array)
        }
        return tempArray
        
        
    }
    
    
    //读取 plist 文件 得到一维数组
    func getEmoticonsWithPath(path:String) ->[WLLEmoticonModel]  {
        let file = emoticonsBundle.pathForResource("\(path)/info.plist", ofType: nil)!
        let array = NSArray(contentsOfFile: file)!
        //创建一个临时可变数组
        var tempArray : [WLLEmoticonModel] = [WLLEmoticonModel]()
        //遍历数组
        for dic in array {
            //创建模型
            let model = WLLEmoticonModel(dict: dic as! [String :AnyObject])
            //设置路径
            model.path = "\(path)/"
            //添加模型到数组中
            tempArray.append(model)
            
        }
        
        return tempArray
    }
    
    
    
    
    
    
    
}












